const assert = require("assert");
const koffi = require("koffi");

const lib = koffi.load("/my-lib/libapi.so");

const AdditionResult = koffi.struct("AdditionResult", {
    result: "unsigned int",
    errorCode: "unsigned int"
});
const additionFunc = lib.func("AdditionResult addition(unsigned int, unsigned int)");

const result = additionFunc(1, 2);
console.log("Result: %s", JSON.stringify(result));
assert(3 === result.result);
assert(0 === result.errorCode);

testApi();

function testApi() {
    const UINT_MAX = 4294967295;

    function assertAddition(a, b, expectedResult) {
        const result = additionFunc(a, b);

        assert(expectedResult.result === result.result);
        assert(expectedResult.errorCode === result.errorCode);
    }

    assertAddition(0, 0, { result: 0, errorCode: 0 });
    assertAddition(0, 1, { result: 1, errorCode: 0 });
    assertAddition(0, UINT_MAX, { result: UINT_MAX, errorCode: 0 });
    assertAddition(1, 0, { result: 1, errorCode: 0 });
    assertAddition(1, 1, { result: 2, errorCode: 0 });
    assertAddition(1, UINT_MAX - 1, { result: UINT_MAX, errorCode: 0 });
    assertAddition(1, UINT_MAX, { result: 0, errorCode: 1 });
}
